package com.bilin.coin.utils;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;

import java.util.UUID;

/** 设备信息工具类
 * Created by LG on 2017/1/11.
 */

public class MobilePhoneDeviceInfo {
    /**
     *  wifimac地址
      * @param mContext
     * @return
     */
   public static String getWifiMac(Context mContext){
       WifiManager mWifi=(WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
       WifiInfo mConnectionInfo = mWifi.getConnectionInfo();
       String mWifiMac=mConnectionInfo.getMacAddress();
       if(!TextUtils.isEmpty(mWifiMac)){
           Log.e("mac",""+mWifiMac);
           return mWifiMac;
       }
       return "";
   }

    /**
     * 获取IMEI
     * @param mContext
     * @return
     */
    public static String getIMEI(Context mContext){
        TelephonyManager mTelephony = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
        //String mIMEI=mTelephony.getDeviceId();
        String mIMEI=mTelephony.getMeid();
        if(!TextUtils.isEmpty(mIMEI)){
            Log.e("IMEI",""+mIMEI);
            return mIMEI;
        }
        return "";
    }

    /**
     *  Pseudo-Unique ID, 这个在任何Android手机中都有效 解决手机中IMEI获取不到情况，兼容所有手机
     */
    public static String getIMEINew(Context context) {
        //we make this look like a valid IMEI
        String m_szDevIDShort = "35" +
                Build.BOARD.length()%10 +
                Build.BRAND.length()%10 +
                Build.CPU_ABI.length()%10 +
                Build.DEVICE.length()%10 +
                Build.DISPLAY.length()%10 +
                Build.HOST.length()%10 +
                Build.ID.length()%10 +
                Build.MANUFACTURER.length()%10 +
                Build.MODEL.length()%10 +
                Build.PRODUCT.length()%10 +
                Build.TAGS.length()%10 +
                Build.TYPE.length()%10 +
                Build.USER.length()%10 ; //13 digits
        return m_szDevIDShort;
    }

    public static String getUUID() {

        String serial = null;

        String m_szDevIDShort = "35" +
                Build.BOARD.length() % 10 + Build.BRAND.length() % 10 +

                Build.CPU_ABI.length() % 10 + Build.DEVICE.length() % 10 +

                Build.DISPLAY.length() % 10 + Build.HOST.length() % 10 +

                Build.ID.length() % 10 + Build.MANUFACTURER.length() % 10 +

                Build.MODEL.length() % 10 + Build.PRODUCT.length() % 10 +

                Build.TAGS.length() % 10 + Build.TYPE.length() % 10 +

                Build.USER.length() % 10; //13 位

        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                serial = android.os.Build.getSerial();
            } else {
                serial = Build.SERIAL;
            }
            //API>=9 使用serial号
            return new UUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString();
        } catch (Exception exception) {
            //serial需要一个初始化
            serial = "serial"; // 随便一个初始化
        }
        //使用硬件信息拼凑出来的15位号码
        return new UUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString();
    }


    /**
     * 序列号
     * @param mContext
     * @return
     */
    public static String getSerialNumber(Context mContext){
        TelephonyManager mTelephony = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
        String sn = mTelephony.getSimSerialNumber();
        if(!TextUtils.isEmpty(sn)){
            Log.e("sn",""+sn);
            return sn;
        }
        return "";
    }

    /**
     * 获取手机品牌
     *
     * @return
     */
    public static String getPhoneBrand() {
        Log.e("PhoneBrand",""+android.os.Build.BRAND);
        return android.os.Build.BRAND;
    }

    /**
     * 获取手机型号
     *
     * @return
     */
    public static String getPhoneModel() {
        Log.e("PhoneModel",""+android.os.Build.MODEL);
        return android.os.Build.MODEL;
    }
    /**
     * 判断当前设备是手机还是平板，代码来自 Google I/O App for Android
     * @param context
     * @return 平板返回 True，手机返回 False
     */
    public static boolean isTablet(Context context) {
        return (context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE;
    }

    public static String getVersionName(Context context) {
        return getPackageInfo(context).versionName;
    }

    //版本号
    public static int getVersionCode(Context context) {
        return getPackageInfo(context).versionCode;
    }

    private static PackageInfo getPackageInfo(Context context) {
        PackageInfo pi = null;

        try {
            PackageManager pm = context.getPackageManager();
            pi = pm.getPackageInfo(context.getPackageName(),
                    PackageManager.GET_CONFIGURATIONS);

            return pi;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return pi;
    }

    public static String getDeviceId(Context context) {
        StringBuilder deviceId = new StringBuilder();
        // 渠道标志
        //deviceId.append("a");
        try {

            //硬件信息生成
            String hardCode = getIMEINew(context);
            if (!TextUtils.isEmpty(hardCode)) {
                return "hardCode"+hardCode;
            }

            //wifi mac地址
            WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
            WifiInfo info = wifi.getConnectionInfo();
            String wifiMac = info.getMacAddress();
            if(!TextUtils.isEmpty(wifiMac)){
                deviceId.append("wifi");
                deviceId.append(wifiMac);
                LogUtlis.e("getDeviceId : ", deviceId.toString());
                return deviceId.toString();
            }
            //IMEI（imei）
            TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            String imei = tm.getDeviceId();
            if(!TextUtils.isEmpty(imei)){
                deviceId.append("imei");
                deviceId.append(imei);
                LogUtlis.e("getDeviceId : ", deviceId.toString());
                return deviceId.toString();
            }
            //序列号（sn）
            String sn = tm.getSimSerialNumber();
            if(!TextUtils.isEmpty(sn)){
                deviceId.append("sn");
                deviceId.append(sn);
                LogUtlis.e("getDeviceId : ", deviceId.toString());
                return deviceId.toString();
            }

            //如果上面都没有， 则生成一个id：随机码
            String uuid = genUUID();
            if(!TextUtils.isEmpty(uuid)){
                deviceId.append("uuid");
                deviceId.append(uuid);
                LogUtlis.e("getDeviceId : ", deviceId.toString());
                return deviceId.toString();
            }
        } catch (Exception e) {
            e.printStackTrace();
            deviceId.append("uuid").append(getUUID());
        }
        LogUtlis.e("getDeviceId : ", deviceId.toString());
        return deviceId.toString();
    }
    /**
     * 生成一个uuid缓存
     */
    public static String genUUID(){
        String uuid = SharedPreferencesUtils.getInstance().getString("genCacheUUID","");

        if(TextUtils.isEmpty(uuid)){
            uuid = UUID.randomUUID().toString();
            SharedPreferencesUtils.getInstance().saveData("genCacheUUID",uuid);
        }
        LogUtlis.e("genCacheUUID : " + uuid);
        return uuid;
    }
}
